﻿using System.Linq;
using System.Web.Mvc;
using ECatalog.Models;
using ECatalog.Models.Organizations;
using ECatalog.Models.Shops;
using ECatalog.Models.UserModel;

namespace ECatalog.Controllers
{
    public class ShopController : Controller
    {
        private readonly IShopRepository shopRepository;
        private readonly IShopBuilder shopBuilder;
        private readonly IOrganizationRepository organizationRepository;
        private readonly IUserRepository userRepository;

        public ShopController(IShopRepository shopRepository, IOrganizationRepository organizationRepository, IShopBuilder shopBuilder, IUserRepository userRepository)
        {
            this.shopRepository = shopRepository;
            this.organizationRepository = organizationRepository;
            this.shopBuilder = shopBuilder;
            this.userRepository = userRepository;
        }

        [Authorize]
        public ActionResult Index()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var shops = shopRepository.All();
            return View(shops);
        }

        public class OrganizationList
        {
            public string Name { get; set; }
            public int OrganizationId { get; set; }
        }

        [Authorize]
        public ActionResult Create()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var vm = new ShopViewModel
                {
                    Organizations = OrganizationListBuilder()
                };
            return View(vm);
        }

        private OrganizationList[] OrganizationListBuilder()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var organizations = organizationRepository.All().Select(s => new OrganizationList
                {
                    Name = s.Name,
                    OrganizationId = s.OrganizationId
                }).ToArray();
            return organizations;
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ShopViewModel shopViewModel)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (ModelState.IsValid)
            {
                var s = shopBuilder.Build(shopViewModel);
                var shop = shopRepository.Add(s);
                if (shopViewModel.UploadedFile != null)
                {
                    shopBuilder.SaveFile(shopViewModel.UploadedFile, shop);
                }
                return RedirectToAction("Index");
            }
            shopViewModel.Organizations = OrganizationListBuilder();
            return View(shopViewModel);
        }

        [Authorize]
        public ActionResult Edit(int id = 0)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var shop = shopRepository.GetShop(id);
            if (shop == null)
            {
                return HttpNotFound();
            }
            var mv = new ShopViewModel { Shop = shop, Organizations = OrganizationListBuilder() };
            return View(mv);
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ShopViewModel shopViewModel)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (ModelState.IsValid)
            {
                shopRepository.SaveShop(shopViewModel.Shop);
                if (shopViewModel.UploadedFile != null)
                {
                    shopBuilder.SaveFile(shopViewModel.UploadedFile, shopViewModel.Shop);
                }
                return RedirectToAction("Index");
            }
            shopViewModel.Organizations = OrganizationListBuilder();
            return View(shopViewModel);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Delete(int id)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            shopRepository.Delete(id);
            return RedirectToAction("Index");
        }
    }
}